const Address=require('../models/Address')
/**
 * 创建地址
 * @param {String} username 
 * @param {Object} data 
 * @returns Address
 */
async function createAddress(username,data={}){
  const NewAddress=await Address.create({username,...data})
  return NewAddress
}
/**
 * 获取用户所有收货地址
 * @param {String} username 
 * @returns Array
 */
async function getAddressList(username){
  const addressList=await Address.find({username}).sort({ updatedAt: -1 })//按更新时间排序
  // console.log(addressList);
  return addressList
}
/**
 * 获取某个用户的收货地址
 * @param {String} id 
 * @returns Address
 */
async function getAddressById(id){
  const once=await Address.findById(id)
  return once
}
async function updateAddress(id,username,data){
  const address=await Address.findOneAndUpdate(
    {_id:id,username},{username,...data},{new:true})
  return address  
}
module.exports={createAddress,getAddressList,getAddressById,updateAddress}